﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace TPCN.DAL
{
    public class ShoppingCartObj
    {
        private List<ShoppingCartItem> _listproduct;

        public List<ShoppingCartItem> Listproduct
        {
            get { return _listproduct; }
            set { _listproduct = value; }
        }
        private string _Tongtien;

        public ShoppingCartObj()
        {
            _listproduct = new List<ShoppingCartItem>();
            _Tongtien = "0";
        }

        public string Tongtien
        {
            get 
            {
                double sum = 0;
                for (int i = 0; i < _listproduct.Count; i++)
                {
                    sum += _listproduct[i].Thanhtien;
                }
                return sum.ToString("N0"); 
            }
            set 
            { 
                _Tongtien = value; 
            }
        }

        public int Count()
        {
            return _listproduct.Count;
        }

        public void Insert(ShoppingCartItem newItem)
        {
            if ((_listproduct.FindIndex(delegate(ShoppingCartItem obj) { return obj.HangID == newItem.HangID; })) != -1)
            {
                foreach (ShoppingCartItem i in _listproduct)
                {
                    if (i.Equals(newItem))
                    {
                        i.Soluong += newItem.Soluong;
                        return;
                    }
                }
            }
            else
            {
                _listproduct.Add(newItem);
            }
        }

        public void Update(string HangID, int Soluong)
        {
            if (Soluong == 0)
            {
                Delete(HangID);
                return;
            }
            else
            {
                foreach (ShoppingCartItem i in _listproduct)
                {
                    if (i.Equals(HangID))
                    {
                        i.Soluong = Soluong ;
                        return;
                    }
                }
            }
        }

        public void Delete(string HangID)
        {
            int removedItemIndex = _listproduct.FindIndex(delegate(ShoppingCartItem obj) { return obj.HangID == HangID; });
            _listproduct.RemoveAt(removedItemIndex);
        }

    }
}
